package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Properties;

@Service
public class DishServiceImpl implements DishService {

        @Autowired
        private DishMapper dishMapper;

        @Autowired
        private DishFlavorMapper dishFlavorMapper;
        @Autowired
        private SetmealDishMapper setmealDishMapper;

        @Autowired
        private Properties pageHelperProperties;

        /**
         * 新增菜品和对应的口味
         * @param dishDTO
         */
        @Transactional
        public void saveWithFlavor(DishDTO dishDTO) {
            Dish dish = new Dish();
            BeanUtils.copyProperties(dishDTO, dish);
            dishMapper.insert(dish);


            //获取菜品id
             Long dishId = dish.getId();

            List<DishFlavor> flavors = dishDTO.getFlavors();
            if(flavors != null && flavors.size() > 0) {
                    flavors.forEach(flavor -> {
                        flavor.setDishId(dishId);

                    });
            }
            dishFlavorMapper.insertBatch(flavors);


        }
        /**
         * 分页查询菜品
         * @param dishPageQueryDTO
         * @return
         */
        public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
           PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
           Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
           return new PageResult(page.getTotal(), page.getResult());
        }

        /**
         * 删除菜品
         * @param ids
         */
        @Transactional
        public void deleteBatch(List<Long> ids) {
            //判断是否在售
           for (Long id : ids) {
               Dish dish = dishMapper.selectById(id);
               if(dish.getStatus() == StatusConstant.ENABLE) {
                   throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
               }
            }

            //判断是否在套餐中
            List<Long> setmealIds = setmealDishMapper.getDishIdsBySetmealId(ids);
           if(setmealIds != null && setmealIds.size() > 0) {
               throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
           }

            //可以进行删除口味数据以及菜品数据事务处理

//            ids.forEach(id -> {
//                dishMapper.deleteById(id);
//                dishFlavorMapper.deleteByDishId(id);
//            });

            //根据ids删除菜品数据
            dishMapper.deleteByIds(ids);
            //根据ids删除菜品口味数据
            dishFlavorMapper.deleteByDishIds(ids);
        }

        /**
         * 修改菜品状态
         * @param id
         */
        @Transactional
        public void updateStatus(Long id) {
                Dish dish = dishMapper.selectById(id);
                dish.setStatus(dish.getStatus() == StatusConstant.ENABLE ? StatusConstant.DISABLE : StatusConstant.ENABLE);
                Dish updatedDish = new Dish();
                updatedDish.setId(id);
                updatedDish.setStatus(dish.getStatus());
                dishMapper.update(updatedDish);
        }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    public DishVO getById(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        //根据菜品id查询口味
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO
     */
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //根据菜品id删除口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        //根据菜品id插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }
}
